package main.leetcode.offer.firstround.from51to68;

import main.datastruc.ListNode;

public class ex70 {
    public static void main(String[] args) {
        System.out.println(
                new ex70().deleteDuplication(ListNode.creList(new int[] {1, 2, 3, 3, 4, 4, 5})));
        System.out.println(
                new ex70().deleteDuplication(ListNode.creList(new int[] {1, 1, 2, 3, 3, 4, 5, 5})));
    }

    public ListNode deleteDuplication(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummyHead = new ListNode(-1);
        ListNode index = dummyHead;
        //        dummyHead.next = head;
        ListNode tmp = head.next;

        while (tmp != null) {
            if (tmp.val == head.val) {
                while (tmp != null && tmp.val == head.val) {
                    tmp = tmp.next;
                }
                if (tmp == null) {
                    head = null;
                    break;
                }
            } else {
                index.next = head;
                index = index.next;
            }
            head = tmp;
            tmp = head.next;
        }
        index.next = head;
        return dummyHead.next;
    }
}
